<?php

class NewsViewModel extends Model
{


    public function getList($where, $page, $limit,$view)
    {
        $pre = C("DB_PREFIX");
        $sql = "SELECT a.*,b.title AS category,c.title AS site,c.sitehash FROM {$pre}news AS a,{$pre}category AS b, {$pre}site AS c WHERE a.categoryhash=b.categoryhash AND b.sitehash=c.sitehash ";
        $sql .= isset($where['category']) ? "AND a.categoryhash={$where['category']} ":"";
        $sql .= isset($where['site']) ? "AND b.sitehash={$where['site']} ":"";
        $sql .= isset($where['author']) ? "AND a.author='{$where['author']}' ":"";
        $sql .= $view=="image" ? "AND a.thumb!=0 ":"";
        $sql .= "ORDER BY a.date DESC LIMIT ".($page-1)*$limit.",".$limit;

       return $this->query($sql);
    }




    public function count($where,$view)
    {
         $pre = C("DB_PREFIX");
        $sql = "SELECT COUNT(a.newshash) as cnt FROM {$pre}news AS a,{$pre}category AS b, {$pre}site AS c WHERE a.categoryhash=b.categoryhash AND b.sitehash=c.sitehash ";
        $sql .= isset($where['category']) ? "AND a.categoryhash={$where['category']} ":"";
        $sql .= isset($where['site']) ? "AND b.sitehash={$where['site']} ":"";
        $sql .= isset($where['author']) ? "AND a.author='{$where['author']}' ":"";
        $sql .= $view=="image" ? "AND a.thumb!=0 ":"";

        $ret = $this->query($sql);
        return $ret?intval($ret[0]["cnt"]):0;
    }


    public function getNews($newshash)
    {
        $pre = C("DB_PREFIX");
        $sql = "SELECT a.*,b.title AS category,c.title AS site,c.sitehash FROM {$pre}news AS a,{$pre}category AS b, {$pre}site AS c WHERE a.categoryhash=b.categoryhash AND b.sitehash=c.sitehash AND a.newshash={$newshash}";
        
        $ret = $this->query($sql);
        return $ret[0];    
    }


    public function incView($newshash)
    {
         $pre = C("DB_PREFIX");
         $sql = "UPDATE {$pre}news SET view=view+1 WHERE newshash={$newshash}";

         return $this->query($sql);
    }

    public function incComment($newshash)
    {
         $pre = C("DB_PREFIX");
         $sql = "UPDATE {$pre}news SET comment=comment+1 WHERE newshash={$newshash}";

         return $this->query($sql);
    }


    public function getPreNews($news)
    {
        $pre = C("DB_PREFIX");
       $sql = "SELECT newshash,title,date FROM {$pre}news WHERE categoryhash={$news['categoryhash']} AND  date>{$news['date']} AND newshash!={$news['newshash']}  ORDER BY date ASC LIMIT 1";
        $ret = $this->query($sql);

        return $ret==false ? false : $ret[0];

    }

      public function getNextNews($news)
    {
        $pre = C("DB_PREFIX");
        $sql = "SELECT newshash,title,date FROM {$pre}news WHERE categoryhash={$news['categoryhash']} AND  date<{$news['date']} AND newshash!={$news['newshash']}  ORDER BY date DESC LIMIT 1";
        $ret = $this->query($sql);

        return $ret==false ? false : $ret[0];

    }

}

?>